﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ASCore;
using FXnaGis;

using FXnaAppBase;

namespace ASApplication
{

    /// <summary>
    /// 设计用于监听主程序事件，并根据这些事件来更新gis绘图操作
    /// </summary>
    public class GisProjectListener
    {

        public GisProjectListener(FMap map)
        {

            this._currentMap = map;

            this._layerService = this._currentMap.GeoMapFrame.LayerService;

 
        }


        FMap _currentMap;


        LayerService _layerService;


        public void ListenProject(Project project)
        {

            project.CommonData.Images.ImageAdded += new EventHandler(Images_ImageAdded);

            project.CommonData.Images.ImageRemoved += new EventHandler(Images_ImageRemoved);


            project.CommonData.VectorLayers.EntityAdded += new EventHandler<FXnaInfrastructure.EntityEventArgs>(VectorLayers_EntityAdded);
            project.CommonData.VectorLayers.EntityRemoved += new EventHandler<FXnaInfrastructure.EntityEventArgs>(VectorLayers_EntityRemoved);







        }

        void VectorLayers_EntityRemoved(object sender, FXnaInfrastructure.EntityEventArgs e)
        {

            FGroupLayer group = e.Entity as FGroupLayer;


            GisService.CurrentService.CurrentMap.GeoMapFrame.SuspendEvents();

            GisService.CurrentService.CurrentMap.GeoMapFrame.LayerService.Remove(group);

            GisService.CurrentService.CurrentMap.GeoMapFrame.ResumeEvents();



            GisService.CurrentService.CurrentMap.ZoomToMaxExtent();


           
        }

        void VectorLayers_EntityAdded(object sender, FXnaInfrastructure.EntityEventArgs e)
        {
            //然后在再GIS里显示，这里存储了两个相关的句柄，之后可能会改；


            FGroupLayer group = e.Entity as FGroupLayer;


            GisService.CurrentService.CurrentMap.GeoMapFrame.SuspendEvents();

            GisService.CurrentService.CurrentMap.GeoMapFrame.LayerService.Add(group);

            GisService.CurrentService.CurrentMap.GeoMapFrame.ResumeEvents();


            GisService.CurrentService.CurrentMap.ZoomToLayer(group);




        }

       

        void Images_ImageRemoved(object sender, EventArgs e)
        {
            string imagePath = ProjectService.CurrentService.GetProjPath(ProjPath.Images);

            Bitmap bm = sender as Bitmap;

            FGroupLayer group = this._currentMap.GeoMapFrame.LayerService.GetLayer(ASCoreNames.ImageLayers) as FGroupLayer;

            FBitmapLayer blayer = group.GetLayer(bm.FileNameWithoutExt) as FBitmapLayer;


            group.Remove(blayer);
        }

        void Images_ImageAdded(object sender, EventArgs e)
        {
            string imagePath = ProjectService.CurrentService.GetProjPath(ProjPath.Images);

            Bitmap bm = sender as Bitmap;

            FGroupLayer group = this._currentMap.GeoMapFrame.LayerService.GetLayer(ASCoreNames.ImageLayers) as FGroupLayer;

            FBitmapLayer blayer = new FBitmapLayer(bm.FileNameWithoutExt, imagePath + bm.FileName, bm.Extent);

            group.Add(blayer);
        }




       


    }
}
